package com.example.controller;

import com.example.common.BaseResponse;
import com.example.common.ErrorCode;
import com.example.common.ResultUtils;
import com.example.exception.BusinessException;
import com.example.model.PageBean;
import com.example.model.room.RoomAddRequest;
import com.example.model.room.RoomAllInfo;
import com.example.model.user.User;
import com.example.service.RoomService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.contant.UserConstant.*;

@RestController
@Slf4j
@RequestMapping("/room")
public class RoomController {
    @Resource
    private RoomService roomService;

    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */
    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
//        if (user == null || (user.getUserRole() != ADMIN_ROLE && user.getUserRole() != SUPER_ADMIN_ROLE))
//            return false;
//        return true;
        if (userObj == null || !(userObj instanceof User)) {
            // userObj 是 null 或者不是 User 类型，返回 false
            return false;
        }
        // 检查用户角色是否为 1 或 2
        return user.getUserRole() == ADMIN_ROLE || user.getUserRole() == SUPER_ADMIN_ROLE;
    }

    /**
     * 新增房间
     * @param roomAddRequest 新增房间实体
     * @param request 请求
     * @return 房间id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addRoom(@RequestBody RoomAddRequest roomAddRequest, HttpServletRequest request) {
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        Long result=roomService.add(roomAddRequest);
        return ResultUtils.success(result);
    }

    /**
     * 更新房间信息
     * @param roomInfo 房间信息
     * @param request 请求
     * @return 是否更新成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateHotel(@RequestBody RoomAddRequest roomInfo, HttpServletRequest request){
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        Boolean result=roomService.update(roomInfo);
        if(!result)
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据酒店id查询房间
     * @param id 酒店id
     * @return 房间集合
     */
    @GetMapping("/search/hotelId")
    public BaseResponse<List<RoomAllInfo>> getRoomByHotelId(Long id){
        List<RoomAllInfo> roomList=roomService.getByHotelId(id);
        return ResultUtils.success(roomList);
    }

    @GetMapping("/search")
    public BaseResponse<PageBean> getRoomAll( @RequestParam(value = "hotelId", required = false,defaultValue = "") Long hotelId,
                                              @RequestParam(value = "type", required = false,defaultValue = "") Integer type,
                                              @RequestParam(value = "minPrice", required = false,defaultValue = "") Integer minPrice,
                                              @RequestParam(value = "maxPrice", required = false,defaultValue = "") Integer maxPrice,
                                              @RequestParam(value = "city", required = false,defaultValue = "") String city,
                                              @RequestParam(defaultValue = "1") Integer pageNumber,
                                             @RequestParam(defaultValue = "10")Integer pageSize,
                                             HttpServletRequest request){
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        PageBean pageBean =roomService.getRoomAll(pageNumber,pageSize,hotelId,type,minPrice,maxPrice,city);
        return ResultUtils.success(pageBean);
    }

    @GetMapping("/search/other")
    public BaseResponse<PageBean> searchRooms(
            @RequestParam(value = "hotelId", required = false,defaultValue = "") Long hotelId,
            @RequestParam(value = "type", required = false,defaultValue = "") Integer type,
            @RequestParam(value = "minPrice", required = false,defaultValue = "") Integer minPrice,
            @RequestParam(value = "maxPrice", required = false,defaultValue = "") Integer maxPrice,
            @RequestParam(defaultValue = "1") Integer pageNumber,
            @RequestParam(defaultValue = "10")Integer pageSize) {
        PageBean pageBean = roomService.searchRooms(hotelId,type, minPrice, maxPrice,pageNumber,pageSize);
        return ResultUtils.success(pageBean);
    }

}
